﻿using System;
using System.Data;
using System.Configuration;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.ComponentModel;

namespace TA.Turbo.Controls
{
	/// <summary>
	/// Summary description for TextBox
	/// </summary>
	public class TextBox : BaseFieldControl<string>
	{
		#region PUBLIC
		#region INIT
		/// <summary>
		/// Ctor.
		/// </summary>
		public TextBox()
		{
			Mode = TextBoxMode.SingleLine;
		}
		#endregion
		#region PROPS
		#region DEF
		/// <summary>
		/// Props.
		/// </summary>
		protected static new Properties _clientProperties = Define(BaseFieldControl<string>._clientProperties,
			new BasePropertyDefinition[] {
				new PropertyDefinition<bool>("ReadOnly", "RO", false) { IsHTML = true },
				new PropertyDefinition<int>("MaxHeight", "MH", 150) { IsHTML = true },
			});
		/// <summary>
		/// Props
		/// </summary>
		public override Properties ClientProperties
		{
			get
			{
				return _clientProperties;
			}
		}
		#endregion
		/// <summary>
		/// The mode in which this control will be rendered and operated.
		/// </summary>
		[DefaultValue(TextBoxMode.SingleLine)]
		public TextBoxMode Mode { get; set; }
		/// <summary>
		/// Maximum height if <see cref="Mode"/> is <see cref="TextBoxMode.ExpandableMultiLine"/>.
		/// </summary>
		[DefaultValue(150)]
		public int MaxHeight
		{
			get
			{
				return GetProperty<int>("MaxHeight");
			}
			set
			{
				ViewState["MaxHeight"] = value;
			}
		}
		/// <summary>
		/// If true, this text box will be read-only.
		/// </summary>
		[DefaultValue(false)]
		public bool ReadOnly
		{
			get
			{
				return GetProperty<bool>("ReadOnly");
			}
			set
			{
				ViewState["ReadOnly"] = value;
			}
		}
		/// <summary>
		/// Gets true (because text box is focusable).
		/// </summary>
		public override bool CanReceiveFocus
		{
			get
			{
				return true;
			}
		}
		#endregion
		#endregion
		#region INTERNAL
		const string OnKeyUpScript = @"if(this.scrollHeight > this.offsetHeight) $(this).set_Height(Math.min(parseInt(this.getAttribute('MaxHeight')), this.scrollHeight + 10));";
		#region METHODS
		#region INIT
		/// <summary>
		/// Init.
		/// </summary>
		/// <param name="e"></param>
		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);

		}
		/// <summary>
		/// Registers InputControl.js and TextBox.js.
		/// </summary>
		protected internal override void RegisterIncludes(IncludeCollection includes)
		{
			base.RegisterIncludes(includes);
			includes.Add(new ScriptResourceInclude("TA.Turbo.Res.JS.Simple.InputControl.js", typeof(BaseCommandControl)));
			includes.Add(new ScriptResourceInclude("TA.Turbo.Res.JS.Simple.TextBox.js", typeof(BaseCommandControl)));
			if (this.Mode == TextBoxMode.ExpandableMultiLine)
				includes.Add(Script.CSS);
		}
		#endregion
		#region RENDER
		/// <summary>
		/// Adds type, readonly, onkeyup and MaxHeight attributes.
		/// </summary>
		/// <param name="writer"></param>
		protected override void AddAttributesToRender(HtmlTextWriter writer)
		{
			base.AddAttributesToRender(writer);
			switch (Mode)
			{
				case TextBoxMode.ExpandableMultiLine:
					writer.AddAttribute("onkeyup", TPage.PrepareScript(OnKeyUpScript));
					writer.AddAttribute("MaxHeight", this.MaxHeight.ToString());
					break;
				case TextBoxMode.SingleLine:
					writer.AddAttribute(HtmlTextWriterAttribute.Type, "text");
					break;
				case TextBoxMode.Password:
					writer.AddAttribute(HtmlTextWriterAttribute.Type, "password");
					break;
			}

			if (this.ReadOnly)
				writer.AddAttribute(HtmlTextWriterAttribute.ReadOnly, "readonly");
		}
		protected override void AddValueAttributes(HtmlTextWriter writer)
		{
			switch (this.Mode)
			{
				case TextBoxMode.SingleLine:
				case TextBoxMode.Password:
					base.AddValueAttributes(writer);
					break;
			}
		}
		/// <summary>
		/// Renders the begin tag which can be <see cref="HtmlTextWriterTag.Textarea"/> or <see cref="HtmlTextWriterTag.Input"/>.
		/// </summary>
		/// <param name="writer"></param>
		protected override void RenderBeginTag(HtmlTextWriter writer)
		{
			switch (Mode)
			{
				case TextBoxMode.MultiLine:
				case TextBoxMode.ExpandableMultiLine:
					Tag = HtmlTextWriterTag.Textarea; break;
				case TextBoxMode.SingleLine:
				case TextBoxMode.Password:
					Tag = HtmlTextWriterTag.Input; break;
			}
			base.RenderBeginTag(writer);
		}
		protected override void RenderContents(HtmlTextWriter writer)
		{
			base.RenderContents(writer);

			switch (this.Mode)
			{
				case TextBoxMode.MultiLine:
				case TextBoxMode.ExpandableMultiLine:
					writer.Write(this.Value);
					break;
			}
		}
		#endregion
		#endregion
		#endregion
	}
}